package liumengbaio.lolzhangyoubao.Activity.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.Adapter.RollPagerAdapter;
import liumengbaio.lolzhangyoubao.Activity.Adapter.XrecycleviewAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.AdPic;
import liumengbaio.lolzhangyoubao.Activity.InFo.ZhangYouBao;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/8/24.
 */
public class InfoPlay extends Fragment{
    private String pathPhote = "http://lol.zhangyoubao.com/apis/rest/" +
            "ItemsService/ads?&i_=EAC1B788-00BC-454A-A9B9-460852CFC011&t_" +
            "=1438744725&p_=16520&v_=40050303&d_=ios&osv_=8.3&version=0&a_=lol";

    private String path_next;
    private RollPagerView info_rollPager;
    private XRecyclerView info_scrollListView;
    private AdPic adPic = new AdPic();
    private RollPagerAdapter adapter;
    private XrecycleviewAdapter listViewAdapter;
    private List<ZhangYouBao.DataBean> mlist = new ArrayList<>();
    private List<ZhangYouBao.DataBean> tempList = new ArrayList<>();
    private int page = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.into_item, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        getDataWeb();
    }

    private void getDataWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String path = "http://lol.zhangyoubao.com/apis/rest/ItemsService/lists?cattype=news&catid=10179&page=" +
                        page +
                        "&i_=EAC1B788-00BC-454A-A9B9-460852CFC011&t_=1438755014&p_=17387&v_=40050303&d_=ios&osv_=8.3&version=0&a_=lol";
                OkHttpClient client = new OkHttpClient();
                final Request request = new Request.Builder()
                        .url(path)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {

                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        tempList = gson.fromJson(result, ZhangYouBao.class).getData();

                        if (tempList.size() > 0) {
                            handler_zhangyoubao.sendEmptyMessage(1);

                        } else {
                            handler_zhangyoubao.sendEmptyMessage(0);
                        }
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                OkHttpClient client = new OkHttpClient();
                                Request request_adPic = new Request.Builder()
                                        .url(pathPhote)
                                        .build();
                                Call call = client.newCall(request_adPic);
                                call.enqueue(new Callback() {
                                    @Override
                                    public void onFailure(Request request, IOException e) {
                                    }

                                    @Override
                                    public void onResponse(Response response) throws IOException {
                                        String result_adPic = response.body().string();
                                        Gson gson = new Gson();
                                        adPic = gson.fromJson(result_adPic, AdPic.class);
                                        if (adPic.getData().size() > 0) {
                                            handler_AdPic.sendEmptyMessage(1);
                                        } else {
                                            handler_AdPic.sendEmptyMessage(0);
                                        }
                                    }
                                });
                            }
                        }).start();
                    }
                });
            }
        }).start();
    }

    private void initView(View view) {
        info_scrollListView = (XRecyclerView) view.findViewById(R.id.info_scrollListView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        info_scrollListView.setLayoutManager(linearLayoutManager);
        info_scrollListView.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 1;
                getDataWeb();
            }

            //上拉加载
            @Override
            public void onLoadMore() {
                page++;
                getDataWeb();
            }
        });
        info_scrollListView.setFocusable(false);
        info_rollPager = new RollPagerView(getActivity());
        info_rollPager.setPlayDelay(2000);
        info_rollPager.setAnimationDurtion(500);
        LinearLayout.LayoutParams imagebtn_params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, 250);
        info_rollPager.setLayoutParams(imagebtn_params);
        info_rollPager.setHintView(new ColorPointHintView(getActivity(), Color.YELLOW, Color.WHITE));
        info_scrollListView.addHeaderView(info_rollPager);

    }

    private Handler handler_zhangyoubao = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    Toast.makeText(getActivity(), "抱歉，没有数据", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    if (page == 1) {
                        mlist.clear();
                        mlist.addAll(tempList);
                        ;
                        listViewAdapter = new XrecycleviewAdapter(getActivity(), mlist);
                        info_scrollListView.setAdapter(listViewAdapter);
                        info_scrollListView.refreshComplete();
                        tempList.clear();
                    } else {
                        mlist.addAll(tempList);
                        listViewAdapter.notifyDataSetChanged();
                        info_scrollListView.loadMoreComplete();
                        tempList.clear();
                    }


                    //                    info_scrollListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    //                        @Override
                    //                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //                            pamas=zhangyoubao.getData().get(position).getId();
                    //                            path_next = "http://lol.zhangyoubao.com/mobiles/item/" +pamas+
                    //                                    "?user_id=&token=&i_=EAC1B788-00BC-454A-A9B" +
                    //                                    "9-460852CFC011&t_=1438745542&p_=18971&v_" +
                    //                                    "=40050303&d_=ios&osv_=8.3&version=0&a_=lol&size=middle";
                    //                            Log.i("001", path_next);
                    //                            Intent intent = new Intent(getActivity(), InfoDetals.class);
                    //                            Bundle bundle = new Bundle();
                    //                            bundle.putString("url", path_next);
                    //                            intent.putExtras(bundle);
                    //                            startActivity(intent);
                    //
                    //                        }
                    //                    });
            }
        }
    };
    private Handler handler_AdPic = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "抱歉没有数据", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    adapter = new RollPagerAdapter(adPic, getActivity(), info_rollPager);
                    info_rollPager.setAdapter(adapter);
                    break;
            }
        }
    };
}
